package com.object.handler;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.object.model.UserInfo;

public class ClientHandler extends IoHandlerAdapter {
	private final UserInfo ui;
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	public ClientHandler(UserInfo ui){
		this.ui = ui;
	}

	@Override
	public void sessionOpened(IoSession session) throws Exception {
		session.write(ui);
	}
	
	@Override
	public void messageReceived(IoSession session, Object message) throws Exception {
		UserInfo ui = (UserInfo)message;
		logger.info(session.getRemoteAddress().toString());
		logger.info("收到服务机发来的用户名：" + ui.getName());
	}

	@Override
	public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
		logger.info("与" + session.getRemoteAddress() + "通信过程中出现错误:[" + cause.getMessage() + "]..连接即将关闭....");
		session.close(false);
		session.getService().dispose();
	}
}